{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 编程实战 = 编程基础知识 >> 【产品思维】 >> 生活经验\n",
    "\n",
    "# 主要目的：1.替代重复性劳动；2.制作工具解决问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 流程图工具使用 运用visio\n",
    "# 第一遍写代码时，可先不考虑函数或类，先让代码跑起来\n",
    "# 写完之后。如果你发现有哪些可以优化的，大胆尝试。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 帮你做选择之我要吃什么\n",
    "import random\n",
    "# 将需要用到的表格和变量放在开头\n",
    "list_food = ['KFC', '蒸菜馆', '楼下快餐店', '桂林米粉', '东北饺子', '金牌猪脚饭', '三及第汤饭']  # 备选菜单，可自定义。\n",
    "list_choice = []\n",
    "\n",
    "# 由于两个原因都包含判断过程，所以，为了让代码更简洁，可将其封装成函数。\n",
    "def choose(list):\n",
    "    while True:\n",
    "        food = random.choice(list)\n",
    "        judgement = input('去吃【%s】好不好啊？同意的话输入y，不想吃直接回车即可。'%(food))\n",
    "        if judgement == 'y':\n",
    "            print('去吃【%s】！就这么愉快地决定啦！'%(food)) \n",
    "            break\n",
    "# 判断环节\n",
    "reason = int(input('你不知道吃什么的原因是：1.完全不知道吃什么；2.在几家店之间徘徊（请输入1或2）：'))\n",
    "if reason == 1:\n",
    "    choose(list_food)\n",
    "elif reason == 2:\n",
    "    add = True\n",
    "    while add:\n",
    "        choice = input('请输入让你犹豫的店名（注：一家一家输，完成后输入y）：')\n",
    "        if choice != 'y':  # 这个判断语句，是为了不将 y 也添加到菜单里。\n",
    "            list_choice.append(choice)\n",
    "        if choice == 'y':\n",
    "            add = False\n",
    "            choose(list_choice)\n",
    "        else:\n",
    "            print('抱歉，目前还不支持第三种情况——不过，你可以加代码哦。')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, time\n",
    "\n",
    "content = ' 风变编程，陪你一起学Python ' # 广告词可自定义。\n",
    "\n",
    "while True:\n",
    "    os.system('clear')\n",
    "    print(content)\n",
    "    content = content[1:] + content[0]\n",
    "    time.sleep(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 项目目标：富安花园（小区）3栋 2单元 401户；朝向：南北朝向；面积：90平方。\n",
    "# 逐步实现\n",
    "\n",
    "#创建csv文件\n",
    "\n",
    "import csv\n",
    "\n",
    "with open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\18~19关：第6个小目标：完成一个迷你产品\\assets.csv', 'a', newline='') as csvfile:\n",
    "#调用open()函数打开csv文件，传入参数：文件名“assets.csv”、追加模式“a”、newline=''。\n",
    "    csv_f = csv.writer(csvfile, dialect='excel')\n",
    "    # 用csv.writer()函数创建一个writer对象。\n",
    "    header=['小区名称', '地址', '建筑年份', '楼栋', '单元', '户室', '朝向', '面积']\n",
    "    csv_f.writerow(header)\n",
    "    #用writerow()函数将表头写进csv文件里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 收集初始楼栋信息\n",
    "start_floor = input('请输入起始楼层：')\n",
    "end_floor = input('请输入终止楼层：')\n",
    "\n",
    "input('接下来请依次输入起始层每个房间的户室尾号、南北朝向及面积，按任意键继续')\n",
    "\n",
    "start_floor_rooms = {}\n",
    "unit_rooms = {}\n",
    "floor_last_number = []\n",
    "# 收集起始层的房间信息\n",
    "\n",
    "# 定义循环控制量\n",
    "room_loop = True\n",
    "while room_loop:\n",
    "    last_number = input('请输入起始楼层户室的尾号:（如01，02）')\n",
    "    floor_last_number.append(last_number)\n",
    "    #将尾号用append()添加列表里，如floor_last_number = ['01','02']\n",
    "    room_number = int(start_floor + last_number)\n",
    "    #户室号为room_number,由楼层start_floor和尾号last_number组成,如301\n",
    "\n",
    "    direction = int(input('请输入 %d 的朝向(南北朝向输入1，东西朝向输入2)：' % room_number ))\n",
    "    area = int(input('请输入 %d 的面积，单位 ㎡ ：' % room_number))\n",
    "    start_floor_rooms[room_number] = [direction,area]\n",
    "    # 户室号为键，朝向和面积组成的列表为值，添加到字典里，如start_floor_rooms = {301:[1,70]}\n",
    "    \n",
    "    continued = input('是否需要输入下一个尾号？按 n 停止输入，按其他任意键继续：')\n",
    "    #加入打破循环的条件\n",
    "    if continued == 'n':\n",
    "        room_loop = False\n",
    "    else:\n",
    "        room_loop = True\n",
    "\n",
    "# for i in range(int(start_floor)+1,int(end_floor)+1):\n",
    "#     for j in range(1,int(last_number)+1):\n",
    "#         room_number = int(str(i) + '0' + str(j))\n",
    "#         start_floor_rooms[room_number] = start_floor_rooms[int(start_floor + '0' + str(j))]\n",
    "\n",
    "# print(start_floor_rooms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建楼栋信息\n",
    "start_floor = '3'\n",
    "end_floor = '5'\n",
    "floor_last_number = ['01','02','03']\n",
    "# 之前input()输入的数据为str类型\n",
    "\n",
    "start_floor_rooms = {301:[1,80], 302:[1,80], 303:[2,90]}\n",
    "#初始楼层的模版数据\n",
    "\n",
    "\n",
    "\n",
    "unit_rooms = {} #创建一个字典，存储所有楼层的数据\n",
    "unit_rooms[int(start_floor)] = start_floor_rooms\n",
    "for floor in range(int(start_floor)+1,int(end_floor)+1):\n",
    "    floors = {}\n",
    "    for j in range(len(floor_last_number)):\n",
    "        number = int(str(floor)+floor_last_number[j])\n",
    "        info = start_floor_rooms[int(start_floor+floor_last_number[j])]\n",
    "#         floors[int(str(i)+floor_last_number[j])] = start_floor_rooms[int(start_floor+floor_last_number[j])]\n",
    "        floors[number] = info\n",
    "    unit_rooms[floor] = floors\n",
    "\n",
    "print(unit_rooms)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据写入csv\n",
    "\n",
    "import csv\n",
    "\n",
    "unit_rooms = {3: {301: [1, 80], 302: [1, 80], 303: [2, 90]}, \n",
    "              4: {401: [1, 80], 402: [1, 80], 403: [2, 90]}, \n",
    "              5: {501: [1, 80], 502: [1, 80], 503: [2, 90]}}\n",
    "\n",
    "    \n",
    "# title = input('请输入小区名称')\n",
    "# address = input('请输入小区地址：')\n",
    "# year = input('请输入小区建造年份：')\n",
    "# block = input('请输入楼栋号：')\n",
    "# unit = input('请输入单元号：')\n",
    "# dire = [0,'南北','东西']\n",
    "title = 'a'\n",
    "address = 'b'\n",
    "year = 'c'\n",
    "block = 'd'\n",
    "unit = 'e'\n",
    "dire = [0,'南北','东西']\n",
    "with open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\18~19关：第6个小目标：完成一个迷你产品\\assets.csv', 'a', newline='') as csvfile:\n",
    "    csv_f = csv.writer(csvfile, dialect='excel')\n",
    "    for sub_unit in unit_rooms.values():\n",
    "        for room,info in sub_unit.items():\n",
    "            csv_f.writerow([title,address,year,block,unit,room,dire[info[0]],info[1]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 话题应对灵敏度测试\n",
    "\n",
    "# 结构一：导入模块，涉及知识点：【模块】\n",
    "import requests\n",
    "import json\n",
    "import random\n",
    "import time\n",
    "try :\n",
    "    with open(r'E:\\software\\Python\\crawler\\00-风变python\\2-风变python课程资料【基础语法+爬虫精进】\\Python基础语法\\18~19关：第6个小目标：完成一个迷你产品/new_year_questions.json', encoding='utf-8') as file:\n",
    "        questions_json = json.loads(file.read())\n",
    "except FileNotFoundError:\n",
    "    pass\n",
    "\n",
    "questions = questions_json['questions_list']\n",
    "result = questions_json['result']\n",
    "\n",
    "# 打印问题并选择\n",
    "score = 0\n",
    "ansToscore = [4,3,2,1]\n",
    "input('回车键开始测试')\n",
    "for i in range(len(questions)):\n",
    "    print(questions[i]['q'])\n",
    "    for j in questions[i]['a']:\n",
    "        print(j)\n",
    "    ans = int(input('选择你的选项：(输入1-4)'))\n",
    "    score += ansToscore[ans-1]\n",
    "print(score)\n",
    "\n",
    "# 没难度不写了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "r = requests.get('https://static.pandateacher.com/new_year_questions.json')\n",
    "questions = r.json()['questions_list']\n",
    "print(questions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n",
      "<Worksheet \"VGEON\">\n"
     ]
    }
   ],
   "source": [
    "# 去除excel表格中的某些字符\n",
    "from openpyxl import Workbook\n",
    "from openpyxl import load_workbook\n",
    "\n",
    "letters = ['B','C','D','E','F','G']\n",
    "print(letters[0])\n",
    "wb = load_workbook(filename = r'C:\\Users\\GilesYang\\Desktop\\data for IGBT.xlsx')\n",
    "sheet_ranges = wb['VGEON']\n",
    "print(sheet_ranges)\n",
    "for j in range(0,6):\n",
    "    for i in range(2,51):\n",
    "        cell = letters[j] + str(i)\n",
    "        try:\n",
    "            sheet_ranges[cell] = str(sheet_ranges[cell].value[15:20])\n",
    "        except TypeError:\n",
    "            pass\n",
    "wb.save(filename = r'C:\\Users\\GilesYang\\Desktop\\data for IGBT.xlsx')\n",
    "    #     sheet_ranges[cell] = str(sheet_ranges[cell].value[15:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n",
      "<Worksheet \"VGE 11V\">\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# 去除excel表格中的某些字符\n",
    "from openpyxl import Workbook\n",
    "from openpyxl import load_workbook\n",
    "\n",
    "letters = ['B','C','D','E','F','G']\n",
    "print(letters[0])\n",
    "wb = load_workbook(filename = r'C:\\Users\\GilesYang\\Desktop\\data for IGBT.xlsx')\n",
    "sheet_ranges = wb['VGE 11V']\n",
    "print(sheet_ranges)\n",
    "try:\n",
    "    print(sheet_ranges['A2'].value)\n",
    "    sheet_ranges['A2'] = str(sheet_ranges['A2'].value[15:20])\n",
    "    print(sheet_ranges['A2'].value)\n",
    "except TypeError:\n",
    "    pass\n",
    "wb.save(filename = r'C:\\Users\\GilesYang\\Desktop\\data for IGBT.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}